package com.kevin.utils.common;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

public class KnCommonUtils {
	/**
	 * 值类型数据
	 * @param clazz
	 * @return
	 */
	public static boolean isNumber(Class<?> clazz) {
		if(clazz == null) return false;
		if(clazz == Object.class) return false;
		if(clazz == void.class) return false;
		Class<?> superClass = clazz.getSuperclass();
		if(superClass == null) return true;
		return superClass == Number.class;
	}
	
	/**
	 * 根据类型转换参数
	 * @param value
	 * @param clazz
	 * @return
	 * @throws Exception
	 */
	public static Object changeType(Object value,Class<?> clazz) throws Exception{
		return changeType(value, clazz, true);
	}
	
	/**
	 * 根据类型转换参数
	 * @param value
	 * @param clazz
	 * @param nullToZero
	 * @return
	 * @throws Exception
	 */
	public static Object changeType(Object value,Class<?> clazz,boolean nullToZero) throws Exception {
		if(clazz == null) throw new Exception("参数 clazz 不能为空！");
		if(value == null){
			if(isNumber(clazz)){//如果是值类型，就返回相应的值或报错
				if(nullToZero){
					return 0;
				}
				throw new Exception("参数 value 为null,但是值类型不允许为null！");
			}
			return null;//如果不是值类型直接返回null
		}
		if(value.getClass() == clazz){//如果这个类型跟原来的类型相同，则不进行转换
			return value;
		}
		
		if(clazz == String.class){
			return String.valueOf(value);
		}
		//值类型
		if(clazz == Integer.class || clazz == int.class){
			return Integer.parseInt(String.valueOf(value));
		}
		if(clazz == Double.class || clazz == double.class){
			return Double.parseDouble(String.valueOf(value));
		}
		if(clazz == Long.class || clazz == long.class){
			return Long.parseLong(String.valueOf(value));
		}
		if(clazz == Boolean.class || clazz == boolean.class){
            switch (value.toString().toLowerCase())
            {
                case "yes":
                case "true":
                case "1":
                case "on":
                case "是":
                    return true;
                case "no":
                case "false":
                case "0":
                case "":
                case "否":
                default:
                    return false;
            }
		}
		if(clazz == Short.class || clazz == short.class){
			return Short.parseShort(value.toString());
		}
		if(clazz == Float.class || clazz == float.class){
			return Float.parseFloat(value.toString());
		}
		if(clazz == char.class || clazz == Character.class){
			return (char)value;
		}
		if(clazz == byte[].class || clazz == Byte.class){
	        byte[] bytes = null;  
	        try {  
	            ByteArrayOutputStream bo = new ByteArrayOutputStream();  
	            ObjectOutputStream oo = new ObjectOutputStream(bo);  
	            oo.writeObject(value);  	      
	            bytes = bo.toByteArray();  	  
	            bo.close();  
	            oo.close();  
	        } catch (Exception e) {  
	            System.out.println("translation" + e.getMessage());  
	            e.printStackTrace();  
	        }  
	        return bytes;  
		}
		//日期类型
		if(clazz == Date.class){	
			if(value.getClass() == java.sql.Date.class){
				return new Date(((java.sql.Date)value).getTime());
			}else if(value.getClass() == java.sql.Time.class){
				return new Date(((java.sql.Time)value).getTime());
			}else if(value.getClass() == java.sql.Timestamp.class){
				return new Date(((java.sql.Timestamp)value).getTime());
			}else{
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				return sdf.parse(String.valueOf(value));
			}
		}
		System.out.println("根据类型转换参数未进行转换 " + clazz.toString());
		return value;
	}
	
	
	
    @SuppressWarnings("unchecked")
	public static <T> T getDefault(Class<T> t) {
        if (t == Object.class) {
            return null;
        }
        if (t == Integer.class) {
            return t.cast(0);
        }
        if (t == String.class) {
            return t.cast("");
        }
        if (t == boolean.class) {
            Object ret = false;
            return (T)ret;
        }
        if (t == Boolean.class) {
            Object ret = false;
            return (T)ret;
        }
        if (t == BigDecimal.class) {
            return (T)(new BigDecimal(0));
        }
        Class<?> superClass = t.getSuperclass();
        if (superClass == null) {
            Object val = 0;
            return (T)val;
        }
        if (t.getSuperclass().equals(Number.class)) {
            Object val = 0;
            return (T)val;
        }
        return null;
    }

}
